package UserInterfaceApplet;

import UserInterfaceClasses.*;

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;


public class OneToOne_MessageApp extends JApplet implements ActionListener,Runnable
{
	JPanel panel = new JPanel();
	List list = new List(10);
	JTextField textfield = new JTextField();
	JButton button = new JButton();
	Font ver = new Font("Courier",1,12);
	Font ver2 = new Font("Courier",1,14);
	
	JButton exit = new JButton();
	
	// All Strings
	String username;
	String name2;
	String texta;
	String send;
	String receive;
	String password;
	
	// Connectivity
	Socket client;
	BufferedReader fromServername;
	PrintStream toServername;
	BufferedReader fromServermsg;
	PrintStream toServermsg;
	
	// Threads
	Thread t1 = new Thread(this);
	Message msg = new Message();
	Thread t2 = new Thread(msg);
	
	public void init()
	{
		getContentPane().add(panel);
		panel.setLayout(null);
		panel.setBackground(new Color(0));


/*		try
		{
		client = new Socket("127.0.0.1",1001);
		}
		catch(Exception e)
		{
			System.out.println("Socket is not connected.");
		}
*/	
		if (!login()) {
			
		     try {
		    	 
		         getAppletContext().showDocument
		           (new URL(getCodeBase()+"accessdenied.html"),"_top");
		         System.exit(0);
		         }
		     catch (Exception e) {e.printStackTrace(); }
		     }
		   else {
			   
		     }

		panel.add(list);
		list.setBounds(5,5,450,250);
		list.setFont(ver);
		list.add("Chat Applet");
		list.add(" ");
		
		panel.add(textfield);
		textfield.setBounds(5,265,450,25);
		textfield.setFont(ver);
		textfield.setText("start chatting...");
		
		panel.add(button);
		button.setBounds(5,300,450,25);
		button.setText("send");
		button.setFont(ver);
		button.addActionListener(this);
		
		panel.add(exit);
		exit.setBounds(385,335,70,25);
		exit.setFont(ver);
		exit.setText("exit");
		exit.addActionListener(this);
		
		t1.start();
	}
	
	public boolean login() {
		  boolean userValid = false;
		  LoginApplet login = new LoginApplet (new Frame(""));
		  requestFocus();
		  if (login.id) {
		    username = login.username.getText();
		    password = login.password.getText();
//		    userValid = UserInterfaceClasses.Login.validateUser(username , password);
		    System.out.println
		      ("The password for " + username 
		        +  " is " + (userValid?"valid":"invalid"));
		    }
		  else
		    System.out.println
		      ("Cancel was pressed.");
		    
		  login.dispose();
		  return userValid;
	}
	
		
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == button)
		{	
			//displays text to chat window
			//need to get the message from server
			texta = textfield.getText();
			
			list.add(username + " >> " + texta);
			
			try
			{
				fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
				toServermsg = new PrintStream(client.getOutputStream());
				
				toServermsg.println(texta);
				System.out.println("sent");
			}
			catch(Exception vb)
			{System.out.println("Cannot connect to server.");}
			
		}
		
		if (e.getSource() == exit)
		{
			System.exit(0);
		}
		
		
		
	}
	
	public void run()
	{
		try
		{
		fromServername = new BufferedReader(new InputStreamReader(client.getInputStream()));
		toServername = new PrintStream(client.getOutputStream());
		
		// Send a Name
		{
			toServername.println(username);
		}
		
		// Receive a Name
		{
			name2 = fromServername.readLine();
			list.add(name2 + " Enters Chat");
			list.add(" ");
		}
		
		}
		catch(Exception e3)
		{
			System.out.println("Cannot send or receive a name!");
		}
		
		t2.start();
		
	}
	
	



}


